package cn.itcast.item.service.Impl;

import cn.itcast.hmall.dto.common.ResultDTO;
import cn.itcast.item.mapper.ItemMapper;
import cn.itcast.item.pojo.Item;
import cn.itcast.item.pojo.Req;
import cn.itcast.item.service.ItemService;
import cn.itcast.item.vo.PageDTO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mysql.jdbc.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ItemServiceImpl extends ServiceImpl<ItemMapper, Item> implements ItemService {

    @Autowired
    private ItemMapper itemMapper;

    @Override
    public PageDTO getList(Req req) {
        Page<Item> itemPage = new Page<>(req.getPage(), req.getSize());
        LambdaQueryWrapper<Item> wrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNullOrEmpty(req.getName())){
            wrapper.like(Item::getName,req.getName()+"%");
        }
        if(StringUtils.isNullOrEmpty(req.getBrand())){
            wrapper.eq(Item::getBrand,req.getBrand());
        }
        if(StringUtils.isNullOrEmpty(req.getCategory())){
            wrapper.eq(Item::getCategory,req.getCategory());
        }
        if(StringUtils.isNullOrEmpty(req.getName())&&StringUtils.isNullOrEmpty(req.getBrand())&&StringUtils.isNullOrEmpty(req.getCategory())){
            wrapper=null;
        }
        Page<Item> selectPage = itemMapper.selectPage(itemPage, wrapper);
        PageDTO<Item> pageDTO = new PageDTO<>();
        pageDTO.setTotal(selectPage.getTotal());
        pageDTO.setList(selectPage.getRecords());
        return pageDTO;
    }

    @Override
    public ResultDTO myUpdateById(Long id, int status) {
        LambdaQueryWrapper<Item> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Item::getId,id);
        Item item = new Item();
        item.setStatus(status);
        int i = itemMapper.update(item, wrapper);
        if (i<1){
            return ResultDTO.error("修改失败");
        }
        return ResultDTO.ok();
    }

    @Override
    public ResultDTO myUpdateById(Item item) {
        LambdaQueryWrapper<Item> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Item::getId,item.getId());
        int i = itemMapper.update(item, wrapper);
        if (i<1){
            return ResultDTO.error("修改失败");
        }
        return ResultDTO.ok();
    }

    @Override
    public ResultDTO myDeleteById(Long id) {
        LambdaQueryWrapper<Item> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Item::getId,id);
        int delete = itemMapper.delete(wrapper);
        if (delete<1){
            return ResultDTO.error("修改失败");
        }
        return ResultDTO.ok();
    }

    @Override
    public ResultDTO muSave(Item item) {
        int i = itemMapper.insert(item);
        if (i<1){
            return ResultDTO.error("修改失败");
        }
        return ResultDTO.ok();
    }
}
